package com.aifund.app.controller;

import com.aifund.app.entity.vo.upload.UploadImgVo;
import com.aifund.app.file.HttpResult;
import com.aifund.app.service.OssService;
import com.aifund.common.entity.SystemConfig;
import com.aifund.common.enums.config.ConfigEnum;
import com.aifund.common.exception.AiFundException;
import com.aifund.common.result.ResultBody;
import com.aifund.common.service.SystemConfigService;
import com.aifund.common.util.FileUtil;
import io.minio.MinioClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.management.remote.rmi._RMIConnection_Stub;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/upload")
@Api(tags = "文件上传")
@RestController
public class UploadController {

    @Autowired
    OssService ossService;
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    SystemConfigService configService;

    /**
     * 上传头像
     * @return
     */
    @PostMapping("/uploadImg")
    @ApiOperation("上传图片")
    public ResultBody<UploadImgVo> upload(MultipartHttpServletRequest request) {
        Map<String, MultipartFile> fileMap = request.getFileMap();
        MultipartFile file = null;
        if(fileMap.entrySet()==null||fileMap.entrySet().size()==0){
            throw new AiFundException("500","上传文件不能为空");
        }
        for(Map.Entry<String,MultipartFile> entry : fileMap.entrySet()){
            file = entry.getValue();
        }
        String s = ossService.uploadImg(file);
        UploadImgVo vo = new UploadImgVo();
        vo.setFilePath(s);
        return ResultBody.success(vo);
    }

}
